//
//  XMLAnalyser.swift
//  jiweiapp
//
//  Created by Kin Wong on 15/9/16.
//  Copyright (c) 2015年 Kin Wong. All rights reserved.
//

import Foundation
class XMLAnalyser {
    func readOLXML(fileName: String) -> (titles: [String], details: [String]) {
        var title: [String] = []
        var detail: [String] = []
        let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "xml")
        var xmlData = NSData(contentsOfFile: path!)
        if xmlData != nil {
            if var doc:GDataXMLDocument = GDataXMLDocument(data: xmlData, options: 0, error: nil) {
                var messages = doc.nodesForXPath("//message", error: nil) as! [GDataXMLElement]
                for item in messages {
                    let uTitle = item.attributeForName("title").stringValue()
                    let detailElement = item.elementsForName("detail")[0] as! GDataXMLElement
                    let uDetail = detailElement.stringValue()
                    title.append(uTitle)
                    detail.append(uDetail)
                }
            }
        }
        return (title, detail)
    }
    
    func readPhone(fileName: String) -> (name: [String], phone: [String]) {
        var name: [String] = []
        var phone: [String] = []
        let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "xml")
        var xmlData = NSData(contentsOfFile: path!)
        if xmlData != nil {
            if var doc: GDataXMLDocument = GDataXMLDocument(data: xmlData, options: 0, error: nil) {
                //var messages = doc.nodesForXPath("//message", error: nil) as! [GDataXMLElement]
                let phones = doc.rootElement().elementsForName("Contact") as! [GDataXMLElement]
                for user in phones {
                    let uName = user.attributeForName("name").stringValue()
                    let phoneElement = user.elementsForName("Mobile")[0] as! GDataXMLElement
                    let uPhone = phoneElement.stringValue()
                    name.append(uName)
                    phone.append(uPhone)
                }
            }
        }
        return (name, phone)
    }
    
    func readLocalXML(fileName: String) -> (title: [String], details: [String]) {
        var title: [String] = []
        var detail: [String] = []
        let path = getPath(fileName)
        var xmlData = NSData(contentsOfFile: path)
        if xmlData != nil {
            if var doc:GDataXMLDocument = GDataXMLDocument(data: xmlData, options: 0, error: nil) {
                var messages = doc.nodesForXPath("//message", error: nil) as! [GDataXMLElement]
                for item in messages {
                    let uTitle = item.attributeForName("title").stringValue()
                    let detailElement = item.elementsForName("detail")[0] as! GDataXMLElement
                    let uDetail = detailElement.stringValue()
                    title.append(uTitle)
                    detail.append(uDetail)
                }
            }
        }
        return (title, detail)
    }
    
    func getPath(fileName: String) -> String {
        var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        var documents = path[0] as? String
        return documents!.stringByAppendingPathComponent(fileName)
    }
    
    func writeLocalXML(fileName :String, titles: [String], details: [String]) {
        var data: String = "<Saved>\n"
        if titles.count != 0{
            for i in 0...titles.count-1 {
                data += "<message title=\"\(titles[i])\">\n"
                data += "<detail>\(details[i])</detail>\n"
                data += "</message>\n"
            }
        }
        data += "</Saved>"
        let path = getPath(fileName)
        data.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
    }
    
}